package com.ningxun.pagentcrawler.service.config.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ningxun.pagentcrawler.entity.CrawlerConfig;
import com.ningxun.pagentcrawler.entity.config.CrawlerConfigEntity;
import com.ningxun.pagentcrawler.mapper.config.CrawlerConfigMapper;
import com.ningxun.pagentcrawler.service.config.ICrawlerConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;

/**
 * 爬虫配置服务实现类
 * 
 * @author pagent
 * @date 2025-01-XX
 */
@Service
public class CrawlerConfigServiceImpl implements ICrawlerConfigService {
    
    @Autowired
    private CrawlerConfigMapper crawlerConfigMapper;
    
    @Override
    public List<CrawlerConfigEntity> getAllConfigs(CrawlerConfig config) {

            LambdaQueryWrapper<CrawlerConfigEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper
                    .like(config.getConfigName() != null, CrawlerConfigEntity::getConfigName, config.getConfigName())
                    .eq(config.getEnabled() != null, CrawlerConfigEntity::getEnabled, config.getEnabled())
                    .like(config.getSiteName() != null, CrawlerConfigEntity::getSiteName, config.getSiteName());

            // 3. 查询（会自动分页）
            List<CrawlerConfigEntity> list = crawlerConfigMapper.selectList(wrapper);
            return list;

    }
    
    @Override
    public CrawlerConfig getConfigById(String id) {
        try {
            CrawlerConfigEntity entity = crawlerConfigMapper.selectById(id);
            return entity != null ? entity.toCrawlerConfig() : null;
        } catch (Exception e) {
            System.err.println("获取配置失败: " + e.getMessage());
            return null;
        }
    }
    
    @Override
    public void saveConfig(CrawlerConfig config) {
        try {
            CrawlerConfigEntity entity = CrawlerConfigEntity.fromCrawlerConfig(config);
            
            // 设置时间戳
            if (config.getId() == null || config.getId().isEmpty()) {
                // 新增配置
                entity.setCreateTime(LocalDateTime.now());
            }
            entity.setUpdateTime(LocalDateTime.now());
            
            if (config.getId() == null || config.getId().isEmpty()) {
                crawlerConfigMapper.insert(entity);
            } else {
                crawlerConfigMapper.updateById(entity);
            }
        } catch (Exception e) {
            System.err.println("保存配置失败: " + e.getMessage());
            throw new RuntimeException("保存配置失败", e);
        }
    }
    
    @Override
    public void deleteConfig(String id) {
        try {
            crawlerConfigMapper.deleteById(id);
        } catch (Exception e) {
            System.err.println("删除配置失败: " + e.getMessage());
            throw new RuntimeException("删除配置失败", e);
        }
    }
    
    @Override
    public boolean testConfig(CrawlerConfig config) {
        try {
            // 这里可以添加配置验证逻辑
            // 例如：验证URL格式、检查选择器语法等
            if (config.getBaseUrl() == null || config.getBaseUrl().isEmpty()) {
                return false;
            }
            
            // 基本验证通过
            return true;
        } catch (Exception e) {
            System.err.println("测试配置失败: " + e.getMessage());
            return false;
        }
    }
    

} 